﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;


namespace myking
{
    public class controlwin
    {
        #region 外部声明和动态库引用


        const int SW_HIDE = 0;
        const int SW_SHOWNORMAL = 1;
        const int SW_SHOWMINIMIZED = 2;
        const int SW_SHOWMAXIMIZED = 3;
        const int SW_SHOW = 6;

        [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
        static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public extern static IntPtr GetDlgItem(IntPtr hDlg, int nControlID);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        public static extern int FindWindowEx(int hWnd1, int hWnd2, string lpsz1, string lpsz2);

        [DllImport("user32.dll", CharSet = CharSet.Ansi)]
        public static extern int ShowWindow(IntPtr hwnd, int nShowCmd);

        [DllImport("user32.dll", CharSet = CharSet.Ansi)]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", CharSet = CharSet.Ansi)]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll", CharSet = CharSet.Ansi)]
        public static extern long GetWindowThreadProcessId(IntPtr hWnd, ref long lpdwProcessId);

        [DllImport("user32.dll", CharSet = CharSet.Ansi)]
        public static extern int IsWindowVisible(IntPtr hWnd);

        #endregion

        /// <summary>
        /// 根据进程名设置显示状态
        /// </summary>
        /// <param name="proname"></param>
        /// <param name="winset"></param>
        public void setwindow(string proname, int winset)
        {

            if (proname == null || proname == "")
            {
                proname = "BlueStacks App Player";
            }

            IntPtr hwndAfter = FindWindowEx((IntPtr)0, (IntPtr)0, null, proname);
            while (hwndAfter.ToInt32() != 0)
            {
                ShowWindow(hwndAfter, winset);
                hwndAfter = FindWindowEx((IntPtr)0, hwndAfter, null, proname);
            }

        }

        /// <summary>
        /// 显示窗口
        /// </summary>
        /// <param name="proname"></param>
        public void SetwindowShow(string proname)
        {

            setwindow(proname, SW_SHOW);

        }

        /// <summary>
        /// 隐藏窗口
        /// </summary>
        /// <param name="proname"></param>
        public void SetwindowHide(string proname)
        {

            setwindow(proname, SW_HIDE);

        }


        public void getitemdlg(string proname)
        {

            if (proname == null || proname == "")
            {
                proname = "xxx";
            }

            IntPtr hwndAfter = FindWindowEx((IntPtr)0, (IntPtr)0, null, proname);


            IntPtr hwnd = GetDlgItem(hwndAfter, 0);

            SendMessage(hwnd, 245, 0, 0);

            proname = "xxx";

            //        [DllImport("user32.dll", CharSet=CharSet.Auto)]
            //public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

            //public static extern int FindWindow(string lpClassName, string lpWindowName);
            //public static extern int FindWindowEx(int hWnd1, int hWnd2, string lpsz1, string lpsz2);

            //int hwndAfter = FindWindow(null, "commit");

            //IntPtr hwnd = FindWindow(null, "commit");

            //hwnd1 = GetDlgItem(FindWindow(null, "commit")).ToInt32();
            ////hwnd2 = GetDlgItem(hwnd1, 0);
            ////hwnd3 = GetDlgItem(hwnd2, 894);
            //////SendMessage(hwnd3, 194, 0, this.txtInput.Text);
            ////hwnd4 = GetDlgItem(hwnd1, 1);
            //SendMessage(hwnd1, 245, 0, Convert.ToString(0));
        }
    }
}
